# # -*- coding=utf-8
# from qcloud_cos import CosConfig
# from qcloud_cos import CosS3Client
# import hashlib
# import requests
#
# # 正常情况日志级别使用INFO，需要定位时可以修改为DEBUG，此时SDK会打印和服务端的通信信息
# from crawler.connection.tools import retry
# from crawler.utils.func_tools import get_tunnel_ip
#
# '''
# [COS]
#     COS_SECRET_ID=AKIDwjIeqqFFk6HFza977yRHvLaU9YjjpVHs
#     COS_SECRET_KEY=ZwFJ9l8HCuOddpEPM6e9qJciY9VhgUEl
#     COS_REGION=ap-guangzhou
#     COS_BUCKET=shedana-1301064182
#     COS_APPID=1400344059
#     COS_HOME=https://img.shedana.com/
# '''
#
#
# class Cos:
#     client = None
#     conf = None
#
#     def __init__(self):
#         if self.client is None:
#             config = CosConfig(Region='ap-guangzhou', SecretId='AKIDwjIeqqFFk6HFza977yRHvLaU9YjjpVHs',
#                                SecretKey='ZwFJ9l8HCuOddpEPM6e9qJciY9VhgUEl', Token=None, Scheme='https')
#             self.client = CosS3Client(config)
#
#     @retry(retry_times=3, interval=0.5)
#     def upload_scrapy_bytes(self, prefix, img_url, res):
#
#         filetype = img_url.split('.')[-1]
#         cos_key = 'shedana/reptile/' + prefix + hashlib.md5(
#             img_url.encode(encoding='UTF-8')).hexdigest() + '.' + filetype
#         if ~self.client.object_exists(Bucket='shedana-1301064182', Key=cos_key):
#             response = self.client.put_object(Bucket='shedana-1301064182', Key=cos_key, Body=res)
#             if response['ETag'] is None:
#                 raise Exception("图片上传错误:" + img_url)
#         return 'https://img.shedana.com/' + cos_key
#
#     @retry(retry_times=3, interval=0.5)
#     def upload_requests_bytes(self, prefix, img_url, headers, proxies):
#         res = requests.get(url=img_url, headers=headers, proxies=proxies, verify=False, timeout=(30, 30))
#         if res.status_code != 200:
#             raise Exception('响应状态异常: {}'.format(str(res.status_code)))
#
#         filetype = img_url.split('.')[-1]
#         cos_key = 'shedana/reptile/' + prefix + hashlib.md5(
#             img_url.encode(encoding='UTF-8')).hexdigest() + '.' + filetype
#         if ~self.client.object_exists(Bucket='shedana-1301064182', Key=cos_key):
#             response = self.client.put_object(Bucket='shedana-1301064182', Key=cos_key, Body=res.content)
#             if response['ETag'] is None:
#                 raise Exception("图片上传错误:" + img_url)
#         return 'https://img.shedana.com/' + cos_key
